/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core.html;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.taglib.TagUtils;
import org.apache.struts.util.RequestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * <p>Renders an HTML Base element. This tag extends the functionality of <code>org.apache.struts.taglib.html.BaseTag</code>.
 * It adds ability to choose, which path to set into <code>href</code> attribute of base tag. By default it includes server name,
 * port and context path of your application, but it may include full path to page, currently displayed in browser.
 * Also this tag can export calculated value to some scope.
 * </p>
 * <p>
 * Allowed attributes are:
 * <ul>
 * <li>
 * <b>server</b> - server name to use instead of <code>request.getServerName</code>
 * </li>
 * <li>
 * <b>target</b> - target window for this base reference
 * </li>
 * <li>
 * <b>fullPath</b> - whether to set full path to page in <code>href</code> attribute
 * </li>
 * <li>
 * <b>var</b> - name of variable to export calculated value
 * </li>
 * <li>
 * <b>scope</b> - scope to export variable to
 * </li>
 * </ul>
 * </p>
 * <p><a href="BaseTag.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.9 $ $Date: 2006/08/03 10:08:22 $
 * @jsp.tag name="base"
 * body-content="empty"
 * @see org.apache.struts.taglib.html.BaseTag
 */
public class BaseTag extends SimpleTagSupport {

	protected transient final Log log = LogFactory.getLog(BaseTag.class);

	/**
	 * The server name to use instead of <code>request.getServerName</code>
	 */
	protected String server;

	/**
	 * The target window for this base reference
	 */
	protected String target;

	/**
	 * Whether or not to set full path to page in <code>href</code> attribute
	 */
	protected Boolean fullPath = Boolean.FALSE;

	/**
	 * Name of variable to export calculated value
	 */
	protected String var;

	/**
	 * Scope to export variable to
	 */
	protected String scope;

	/**
     * Returns server name
     *
	 * @return server name
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="The server name to use"
	 */
	public String getServer() {
		return server;
	}

    /**
     * Sets server name
     *
     * @param server server name to set
     */
	public void setServer(String server) {
		this.server = server;
	}

	/**
     * Returns target window
     *
     * @return target window
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="The target window for this base reference"
	 */
	public String getTarget() {
		return target;
	}

    /**
     * Sets target window
     *
     * @param target target window to set
     */
	public void setTarget(String target) {
		this.target = target;
	}

	/**
     * Returns whether or not to set full path to page in href attribute
     *
     * @return whether or not to set full path to page in href attribute
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.Boolean"
	 * description="Whether or not to set full path to page in href attribute"
	 */
	public Boolean getFullPath() {
		return fullPath;
	}

    /**
     * Sets whether or not to set full path to page in href attribute
     *
     * @param fullPath whether or not to set full path to page in href attribute
     */
	public void setFullPath(Boolean fullPath) {
		this.fullPath = fullPath;
	}

	/**
     * Returns name of variable that will accept calculated href
     *
     * @return name of variable
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Name of variable to export message"
	 */
	public String getVar() {
		return var;
	}

    /**
     * Sets name of variable that will accept calculated href
     *
     * @param var name of variable to set
     */
	public void setVar(String var) {
		this.var = var;
	}

	/**
     * Returns variable scope
     *
     * @return variable scope
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Scope to export variable to"
	 */
	public String getScope() {
		return scope;
	}

    /**
     * Sets variable scope
     *
     * @param scope variable scope to set
     */
	public void setScope(String scope) {
		this.scope = scope;
	}

    /**
     * Processes the tag
     *
     * @throws JspException
     */
	public void doTag() throws JspException {

		PageContext pageContext = (PageContext) getJspContext();

		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();

		if ( fullPath == null ) {
			fullPath = Boolean.FALSE;
		}


		if ( server == null || server.length() == 0 ) {
			server = request.getServerName();
		}

		String uri = request.getContextPath();
		if ( fullPath.booleanValue() ) {
			uri = request.getRequestURI();
		} else {
			uri += "/";
		}

		StringBuffer tag = new StringBuffer("<base href=\"");
		StringBuffer hrefValue = RequestUtils.createServerUriStringBuffer(request.getScheme(), server, request.getServerPort(), uri);
		tag.append(hrefValue);
		tag.append("\"");
		if ( target != null && target.length() > 0 ) {
			tag.append(" target=\"").append(target).append("\"");
		}

		TagUtils tagUtils = TagUtils.getInstance();
		if ( tagUtils.isXhtml(pageContext) ) {
			tag.append(" />");
		} else {
			tag.append(">");
		}

		if ( var != null ) {
			// save value in specified scope
			int varScope = PageContext.PAGE_SCOPE;
			if ( scope != null ) {
				varScope = tagUtils.getScope(scope);
			}
			pageContext.setAttribute(var, hrefValue.toString(), varScope);

		} else {
			tagUtils.write(pageContext, tag.toString());
		}

	}

}
